package com.phoenix;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lilongfei on 16/6/24.
 */
public class ServerUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        String account = request.getParameter("username");
        String password = request.getParameter("password");
        String kaptcha = request.getParameter("kaptcha");
//        String kaptchaStr= (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//        request.getSession().removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        LoginAuthentication loginAuthentication = new LoginAuthentication(account, password, null, account, password);
//        if(kaptcha.equalsIgnoreCase(kaptchaStr)){
        String ip = request.getRemoteAddr();
        loginAuthentication.setIp(ip);
        //this.getAuthenticationManager().authenticate();
        return this.getAuthenticationManager().authenticate(loginAuthentication);
//        }else {
//            throw new KaptchaErrorException("验证码错误");
//        }
    }
}
